package com.atxiao.demo.service;

import com.atxiao.demo.bean.Account;
import com.atxiao.demo.bean.FlowLog;
import com.atxiao.demo.bean.Result;
import com.atxiao.demo.bean.User;
import com.atxiao.demo.mapper.AccountMapper;
import com.atxiao.demo.mapper.FlowLogMapper;
import com.atxiao.demo.mapper.TransferMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

import static java.lang.Double.parseDouble;

/**
 * 存款
 */
@Service
@Transactional
public class SaveServiceImpl implements SaveService {

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    FlowLogMapper flowLogMapper;


    @Resource
    private TransferMapper mapper;

    /**
     * 根据卡号存入金额
     *
     * @param account_card_num 卡号
     * @param money            存入钱数
     */
    @Transactional
    public Result save(String account_card_num, Double money) {
        //根据卡号 查询账户余额
        Double source_money = selectAccount(account_card_num);
        //判断是否够扣？？？
        if (source_money < money) {
            return Result.errorResult("余额不够啦");
        }
        //修改后金额
        Double total_money = source_money + money;


        Account accountA = new Account();
        accountA.setCardNum(account_card_num);
        accountA.setBalance(total_money);
        //更新账户余额
        updateAccount(accountA);
        System.out.println(accountA);

        // 账户
        Account accountB = new Account();
        accountB.setCardNum(account_card_num);
        accountB.setBalance(total_money);
        //更新账户余额
        updateAccount(accountB);
        System.out.println(accountB);


        //记录流水A
        FlowLog flowLog = new FlowLog();
        flowLog.setCardNum(account_card_num);
        flowLog.setPrice(money);
        flowLog.setBusinessTime(new Date());
        flowLog.setStatus(true);
        flowLog.setBalance(total_money);
        flowLog.setBusinessPlace("线上");
        flowLog.setResult(true);
        // 需要修改
        //flowLog.setOtherCardNum("0");
        System.out.println(flowLog);
        insertFlow(flowLog);
        return Result.success();
    }


    /**
     *
     * @param account 待转入账户
     * @param reduceAccount 待转出账户
     * @param money 金额
     * @return
     */
    @Transactional
    public Result transfer(String account,String reduceAccount,Double money) {
        Result flag;
        //根据卡号查询出来用户
        Account info = accountMapper.selectById(account);
        if (info != null) {

            //查询转款账户余额
            double remainder = mapper.selectRem(reduceAccount);
            if (money > remainder) {
                return Result.errorResult("账户余额不足");
            }

            //流水时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String time = df.format(new Date());// new Date()为获取当前系统时间

            //交易是否成功标志
            int transfer_result = 1;
            //转出标志
            int type = 3;
            String place = "手机银行";

            //表示付钱
            int status = 1;
            //转账流水记录,转完账以后账户剩下的钱
            double reduceBalance = remainder - money;
            //更新用户的余额
            int reduce = mapper.reduceMoney(reduceAccount, reduceBalance);
            //在流水表中进行跟新一条用户转账并且余额减少的操作
            int reduceCount = mapper.reduceIncome(reduceAccount, money, time, status, reduceBalance, type, place, transfer_result, reduceAccount);

            //收款流水记录 表示存钱
            status = 0;
            //转入标志
            type=2;
            //查询转入账余额
            final double opBalance = mapper.selectRem(account);
            //转入账户现在的余额
            double newBalance = opBalance + money;
            //转入账户更新
            int addAccount = mapper.addMoney(account, newBalance);
            //流水表中更新一条对方账户余额增加的操作
            int count = mapper.insertIncome(account, money, time, status, newBalance, type, place, transfer_result, reduceAccount);


            flag = Result.success("成功转入,即将跳转主页");
        } else {
            flag = Result.errorResult("未找到银行卡号");
        }
        return flag;
    }

    /**
     * 插入流水记录
     */
    private void insertFlow(FlowLog flowLog) {
        flowLogMapper.insertBusiness(flowLog);
    }


    /**
     * 更新账户余额记录
     */
    private void updateAccount(Account account) {
        accountMapper.updateAccount(account);
    }


    /**
     * 根据卡号 查询账户余额
     */
    private Double selectAccount(String account_card_num) {
        Account account = accountMapper.selectAccount(account_card_num);
        return account.getBalance();
    }


}
